package com.ijiuchuang.jc_localization.tools.adapter.list;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

/**
 * Created by 王伟 on 2016/4/24.
 */
public class MyBaseHolder {

    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    private Context context;
    private int mLayoutId;

    private MyBaseHolder(Context context, ViewGroup parent, int layoutId,
                         int position)
    {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        this.context = context;
        this.mLayoutId = layoutId;
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        // setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static MyBaseHolder get(Context context, View convertView,
                                   ViewGroup parent, int layoutId, int position)
    {
        if (convertView == null)
        {
            return new MyBaseHolder(context, parent, layoutId, position);
        }
        return (MyBaseHolder) convertView.getTag();
    }

    public View getConvertView()
    {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId)
    {
        View view = mViews.get(viewId);
        if (view == null)
        {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public MyBaseHolder setText(int viewId, String text)
    {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public MyBaseHolder setImageResource(int viewId, int drawableId)
    {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public MyBaseHolder setImageBitmap(int viewId, Bitmap bm)
    {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public MyBaseHolder setImageByUrl(int viewId, String url)
    {
        ImageView view = getView(viewId);
        Glide.with(context)
                .load(url)
                .into(view);
        return this;
    }

    public int getPosition()
    {
        return mPosition;
    }

    public int getLayoutId()
    {
        return mLayoutId;
    }
}
